 clear
set more off
use "D:\IFLS_retire_panel12wide.dta"

merge 1:1 pidlink using "D:cognition20072014.dta"
drop _merge

gen cognition1 = (co07count1 + co10count1)/2
gen cognition2 = (co07count2 + co10count2)/2
gen agegrp = .
replace agegrp = 0 if age1>=50
replace agegrp = 1 if age1>=50 & age1<60
replace agegrp = 2 if age1>=60 & age1<70
replace agegrp = 3 if age1>=70 & age1<80
drop if female1 != female2

local var phyact_heavy phyact_mod phyact_walk livingstd healthcare monthsalary chronic cesdw creditopp appkd village withbank capcenter marriage urban female happy exp trusta trustb trustc trustd arisan arisan_n trustall crime crime_d safe pmnum pmpnum workcasual contract workstate retired livwcld healthinsu healthpercep missingday wv age co07count co10count genhealth smoke houseown edulevel wifeedulevel wiferetired wifehealth rspndnt cognition bmi

reshape long `var' , i(pidlink) j(wave)
destring pidlink, replace force

gen married = .
replace married = 1 if marriage == 2
replace married = 0 if marriage == 1|marriage==0

gen lessedu = .
replace lessedu = 1 if edulevel == 1|edulevel == 2
replace lessedu = 0 if edulevel == 3|edulevel == 4

gen agesq = age*age
gen rural = 1-urban
replace phyact_walk = 8 - phyact_walk
rename phyact_walk phyact

gen skcog = trustd
gen skstr = pmpnum

gen yr14=.
replace yr14= 1 if wave == 2
replace yr14= 0 if wave == 1

gen female14=female*yr14
gen edu14 = lessedu*yr14
gen rural14 = rural*yr14
gen retired14 = retired*yr14

replace bmi = . if bmi<=12
replace bmi = . if bmi > 40
gen underweight = 0
replace underweight = 1 if bmi <=18.5
gen normal = 0
replace normal = 1 if bmi > 18.5 & bmi<23
gen overweight = 0
replace overweight = 1 if bmi>23 & bmi<=25
gen obese = 0
replace obese = 1 if bmi>25

replace trusta = 5-trusta
replace trusta = . if trusta <0
replace trustb = 5-trustb
replace trustb = . if trustb < 0
replace trustc = 5-trustc
replace trustc = . if trustc <0
replace trustd = 5 - trustd
replace trustd = . if trustd<0

gen cogsk = 0.2446*trusta+0.3231*trustb+0.3081*trustc
gen strsk = pmpnum
//spouseeffect
gen spouselessedu = .
replace spouselessedu = 1 if wifeedulevel == 1
replace spouselessedu = 0 if wifeedulevel == 3| wifeedulevel == 4 | wifeedulevel == 2
gen spouselesseduska = spouselessedu*cogsk
gen spouselesseduskb = spouselessedu*strsk

gen wifelessedu=.
replace wifelessedu = 1 if edulevel == 1
replace wifelessedu = 0 if edulevel == 3| edulevel == 4 | edulevel == 2
gen wifeeduska = wifelessedu*cogsk
gen wifeeduskb = wifelessedu*strsk

gen wiferetiredska = wiferetired*cogsk
gen wiferetiredskb = wiferetired*strsk

gen chronicd = .
replace chronicd = 1 if chronic >=1 & chronic !=.
replace chronicd = 0 if chronic == 0

//SUR_all genders
//20220810 when edu is 1-4
gen wifeedulevelska = wifeedulevel*cogsk
gen wifeedulevelskb = wifeedulevel*strsk
gen eduinter = edulevel*wifeedulevel

global x1list wifeedulevel age female rural edulevel livingstd genhealth phyact_mod bmi smoke cesdw
global x2list wifeedulevel age female rural edulevel livingstd genhealth phyact_mod bmi smoke cognition
global x3list wifeedulevel age female rural edulevel livingstd phyact_mod smoke bmi cesdw cognition
global y1list cognition
global y2list cesdw
global y3list genhealth
global sklist cogsk strsk

sureg ($y1list $x1list) ($y2list $x2list) ($y3list $x3list) if female == 1
est store sur0
sureg ($y1list $x1list $sklist) ($y2list $x2list $sklist) ($y3list $x3list $sklist)  if female == 1
est store sur1
sureg ($y1list $x1list $interlist) ($y2list $x2list $interlist) ($y3list $x3list $interlist) if female == 1
est store sur2

sureg ($y1list $x1list) ($y2list $x2list)  ($y3list $x3list)  if female == 0
est store sur3
sureg ($y1list $x1list $sklist) ($y2list $x2list $sklist) ($y3list $x3list $sklist)  if female == 0
est store sur4
sureg ($y1list $x1list $interlist) ($y2list $x2list $interlist) ($y3list $x3list $interlist)  if female == 0
est store sur5

cd "D:\spouseeffect"
esttab sur0 sur1 sur2 sur3 sur4 sur5 using result0816_1.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace

sureg ($y1list $x1list) ($y2list $x2list) ($y3list $x3list), cor
est store sur0
sureg ($y1list $x1list $sklist) ($y2list $x2list $sklist) ($y3list $x3list $sklist) 
est store sur1

cd "D:\spouseeffect"
esttab sur0 sur1 using result0821_1.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace





//pooled
reg $y1list $x1list if female == 1
est store sur1
reg $y2list $x2list if female == 1
est store sur2
reg $y3list $x3list if female == 1
est store sur3
reg $y1list $x1list $sklist if female == 1
est store sur4
reg $y2list $x2list $sklist if female == 1
est store sur5
reg $y3list $x3list $sklist   if female == 1
est store sur6
cd "D:\spouseeffect"
esttab sur1 sur2 sur3 sur4 sur5 sur6  using result0816_f.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace

reg $y1list $x1list if female == 0
est store sur1
reg $y2list $x2list if female == 0
est store sur2
reg $y3list $x3list if female == 0
est store sur3
reg $y1list $x1list $sklist if female == 0
est store sur4
reg $y2list $x2list $sklist if female == 0
est store sur5
reg $y3list $x3list $sklist   if female == 0
est store sur6
cd "D:\spouseeffect"
esttab sur1 sur2 sur3 sur4 sur5 sur6  using result0816_m.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace


reg $y1list $x1list
est store sur1
reg $y2list $x2list
est store sur2
reg $y3list $x3list 
est store sur3
reg $y1list $x1list $sklist 
est store sur4
reg $y2list $x2list $sklist 
est store sur5
reg $y3list $x3list $sklist   
est store sur6
cd "D:\spouseeffect"
esttab sur1 sur2 sur3 sur4 sur5 sur6  using result0821_mf.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace



//20220810 when edu is dummy

global x1list spouselessedu age female rural edulevel livingstd genhealth phyact_mod bmi smoke cesdw 
global x2list spouselessedu age female rural edulevel livingstd genhealth phyact_mod bmi smoke cognition
global x3list spouselessedu age female rural edulevel livingstd phyact_mod smoke bmi cesdw cognition
global y1list cognition
global y2list cesdw
global y3list genhealth
global sklist cogsk strsk
global interlist cogsk strsk spouselesseduska spouselesseduskb

sureg ($y1list $x1list) ($y2list $x2list) ($y3list $x3list) if female == 1
//Breusch-Pagan test of independence: chi2(3) =   256.968, Pr = 0.0000: we do have correlation across equations
est store sur0
sureg ($y1list $x1list $sklist) ($y2list $x2list $sklist) ($y3list $x3list $sklist)  if female == 1
est store sur1
sureg ($y1list $x1list $interlist) ($y2list $x2list $interlist) ($y3list $x3list $interlist)  if female == 1
est store sur2

sureg ($y1list $x1list) ($y2list $x2list) ($y3list $x3list) if female == 0
est store sur3
sureg ($y1list $x1list $sklist) ($y2list $x2list $sklist) ($y3list $x3list $sklist)  if female == 0
est store sur4
sureg ($y1list $x1list $interlist) ($y2list $x2list $interlist) ($y3list $x3list $interlist)  if female == 0
est store sur5

cd "D:\spouseeffect"
esttab sur0 sur1 sur2 sur3 sur4 sur5 using result0816_2.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace

global x1list spouselessedu age female rural edulevel livingstd genhealth phyact_mod bmi smoke cesdw 
global x2list spouselessedu age female rural edulevel livingstd genhealth phyact_mod bmi smoke cognition
global x3list spouselessedu age female rural edulevel livingstd phyact_mod smoke bmi cesdw cognition
global y1list cognition
global y2list cesdw
global y3list genhealth
global sklist cogsk strsk
global interlist cogsk strsk spouselesseduska spouselesseduskb

sureg ($y1list $x1list) ($y2list $x2list) ($y3list $x3list) , cor
//Breusch-Pagan test of independence: chi2(3) =   256.968, Pr = 0.0000: we do have correlation across equations
est store sur0
sureg ($y1list $x1list $sklist) ($y2list $x2list $sklist) ($y3list $x3list $sklist)  
est store sur1
sureg ($y1list $x1list $interlist) ($y2list $x2list $interlist) ($y3list $x3list $interlist)  
est store sur2


cd "D:\spouseeffect"
esttab sur0 sur1 sur2 using result0821_2.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace


reg $y1list $x1list if female == 1
est store sur1
reg $y2list $x2list if female == 1
est store sur2
reg $y3list $x3list if female == 1
est store sur3
reg $y1list $x1list $sklist if female == 1
est store sur4
reg $y2list $x2list $sklist if female == 1
est store sur5
reg $y3list $x3list $sklist   if female == 1
est store sur6
reg $y1list $x1list $interlist if female == 1
est store sur7
reg $y2list $x2list $interlist if female == 1
est store sur8
reg $y3list $x3list $interlist if female == 1
est store sur9
cd "D:\spouseeffect"
esttab sur1 sur2 sur3 sur4 sur5 sur6 sur7 sur8 sur9 using result0816_ff.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace

reg $y1list $x1list if female == 0
est store sur1
reg $y2list $x2list if female == 0
est store sur2
reg $y3list $x3list if female == 0
est store sur3
reg $y1list $x1list $sklist if female == 0
est store sur4
reg $y2list $x2list $sklist if female == 0
est store sur5
reg $y3list $x3list $sklist   if female == 0
est store sur6
reg $y1list $x1list $interlist if female == 0
est store sur7
reg $y2list $x2list $interlist if female == 0
est store sur8
reg $y3list $x3list $interlist if female == 0
est store sur9
cd "D:\spouseeffect"
esttab sur1 sur2 sur3 sur4 sur5 sur6 sur7 sur8 sur9 using result0816_mm.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace

reg $y1list $x1list 
est store sur1
reg $y2list $x2list 
est store sur2
reg $y3list $x3list 
est store sur3
reg $y1list $x1list $sklist 
est store sur4
reg $y2list $x2list $sklist 
est store sur5
reg $y3list $x3list $sklist 
est store sur6
reg $y1list $x1list $interlist
est store sur7
reg $y2list $x2list $interlist 
est store sur8
reg $y3list $x3list $interlist 
est store sur9
cd "D:\spouseeffect"
esttab sur1 sur2 sur3 sur4 sur5 sur6 sur7 sur8 sur9 using result0821_mfmf.csv, b(3) se(2) star(* 0.10 ** 0.05 *** 0.01) replace
